package com.ruoyi.law.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageCondition;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.law.domain.entity.LawPic;
import com.ruoyi.law.service.ILawPicService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author code
 * @since 2023-02-10
 */
@Api(tags = "图片管理")
@RestController
@RequestMapping("/pic")
public class LawPicController extends BaseController {
    @Autowired
    private ILawPicService picService;

    @Anonymous
    @ApiOperation("列表")
    @GetMapping("/list")
    public R<Page<LawPic>> list(PageCondition<LawPic> pageCondition, String type) {
        Page<LawPic> page = picService.lambdaQuery()
                .eq(StrUtil.isNotBlank(type), LawPic::getType, type)
                .page(pageCondition.toPage("sort", false));
        return R.ok(page);
    }

    @Anonymous
    @ApiOperation("详情")
    @GetMapping("/info")
    public R<LawPic> info(Long id) {
        LawPic pic = picService.getById(id);
        return R.ok(pic);
    }

    @ApiOperation("修改")
    @PostMapping("/update")
    public R<Object> update(@RequestBody LawPic lawPic) {
        picService.updateById(lawPic);
        return R.ok();
    }

    @ApiOperation("新增")
    @PostMapping("/save")
    public R<Object> save(@RequestBody LawPic lawPic) {
        picService.save(lawPic);
        return R.ok();
    }

    @ApiOperation("删除")
    @PostMapping("/delete")
    public R<Object> delete(@RequestBody Long id) {
        boolean remove = picService.removeById(id);
        return remove ? R.ok() : R.fail();
    }
}
